package com.haogu.controller;

import com.haogu.pojo.Location;
import com.haogu.pojo.Result;
import com.haogu.pojo.Weather;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import java.util.Map;


@RestController
@RequestMapping("city")
public class LocationController {

    @Value("${yiketianqi.appid}")
    private String appid;
    @Value("${yiketianqi.appsecret}")
    private String appsecret;
    @Value("${server.port}")
    private String port;

    @GetMapping
    public Result city(String city){
        if (city == null){
            city = "郑州";
        }
        String url = "https://apis.map.qq.com/ws/geocoder/v1/?address=" + city +"key=P5JBZ-W7LRL-35TPJ-MAIWW-GQJ4T-YSBZ4";
        RestTemplate restTemplate = new RestTemplate();
        return Result.success(restTemplate.getForObject(url, Map.class));
    }

    @GetMapping("weather")
    public Result qq(String city){
        if (city == null){
            city = "郑州";
        }
        String url = "https://v0.yiketianqi.com/free/day?appsecret="+appsecret+"&appid="+appid+"&city="+city;
        RestTemplate restTemplate = new RestTemplate();
        Weather weather = restTemplate.getForObject(url, Weather.class);
        weather.setWeaImg("http://127.0.0.1:"+port+"/img/"+weather.getWea_img()+".png");
        return Result.success(weather);
    }

    @GetMapping("location")
    public Result location(String city){
        String url = "https://apis.map.qq.com/ws/location/v1/ip?  ip="+ip+"&key="+key+"&city="+city;
        RestTemplate restTemplate = new RestTemplate();
        Location location = restTemplate.getForObject(url, Location.class);
        location.setWeaImg(location.getWeaImg() + ".png");
    }
}
